package com.changgou.goods.controller;


import com.changgou.common.entity.HttpRespond;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("goods/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /***
     * 查询全部数据
     * @return
     */
    @GetMapping
    public HttpRespond<List<Brand>> queryAllBrand(){
        return this.brandService.listBrand();
    }

    /***
     * 根据ID查询品牌数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public HttpRespond<Brand> findById(@PathVariable Integer id){
        return this.brandService.findById(id);
    }

    /***
     * 新增品牌数据
     * @param brand
     * @return
     */
    @PostMapping
    public HttpRespond<Void> add(@RequestBody Brand brand){
        return this.brandService.add(brand);
    }

    /***
     * 修改品牌数据
     * @param brand
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public HttpRespond<Void> update(@RequestBody Brand brand, @PathVariable Integer id){
        //设置ID
        brand.setId(id);
        //修改数据
        return brandService.update(brand);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public HttpRespond<Void> delete(@PathVariable Integer id) {
        return brandService.delete(id);
    }

    /***
     * 多条件搜索品牌数据
     * @param brand
     * @return
     */
    @PostMapping(value = "/search" )
    public HttpRespond<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        return brandService.findList(brand);
    }

    /***
     * 分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public HttpRespond<PageInfo<Brand>> findPage(@PathVariable  int page, @PathVariable  int size){
        // 分页查询
        return brandService.findPage(page, size);
    }

    /***
     * 分页搜索实现
     * @param brand
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public HttpRespond<PageInfo<Brand>> findPage(@RequestBody(required = false) Brand brand, @PathVariable int page, @PathVariable int size){
        // 执行搜索
        return brandService.findPage(brand, page, size);
    }

    /**
     * 根据分类名称查询品牌列表
     * @param category
     * @return
     */
    @GetMapping("/category")
    public HttpRespond<List<Map>> findListByCategoryName(@RequestParam(name = "category") String category){
        return this.brandService.findListByCategoryName(category);
    }

}
